package com.qf.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 日志信息配置文件
 */
@Aspect
@Component
public class LoggerConfig {
    private static final Logger logger = (Logger) LoggerFactory.getLogger(LoggerConfig.class);

    @Pointcut(value = "execution(* *.*.service.impl.*.*(..))")
    public void myPointcut(){

    }

    @Around(value = "myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint){
        //1.开始记录日志
        logger.debug("==========开始记录日志=========="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //2.记录时间+执行方法名称
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        logger.debug("当前执行的方法是:{}"+methodSignature.getMethod().getName());
        Object proceed = null;
        try {
            proceed = proceedingJoinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("方法执行异常{}"+throwable.getMessage());
        }finally{
            logger.error("==========方法执行结束=========="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        return proceed;

    }
}
